<?php 
if ( ! function_exists('mysql_to_th'))
{
	function mysql_to_th($datetime,$format = 'S' ,$time = FALSE,$nodate = FALSE)
	{
		if($datetime == '0000-00-00' || $datetime=='')return false;
		if($format == 'F')
		{
			$month_th = array( 1 =>'มกราคม',2 => 'กุมภาพันธ์',3=>'มีนาคม',4=>'เมษายน',5=>'พฤษภาคม',6=>'มิถุนายน',7=>'กรกฏาคม',8=>'สิงหาคม',9=>'กันยายน',10=>'ตุลาคม',11=>'พฤศจิกายน',12=>'ธันวาคม');
		}
		else
		{
			$month_th = array( 1 =>'ม.ค.',2 => 'ก.พ.',3=>'มี.ค.',4=>'เม.ย',5=>'พ.ค.',6=>'มิ.ย',7=>'ก.ค.',8=>'ส.ค.',9=>'ก.ย.',10=>'ต.ค.',11=>'พ.ย.',12=>'ธ.ค.');
		}
		
		$datetime = mysql_to_unix($datetime);
		
		$r = date('d', $datetime).' '.$month_th[date('n', $datetime)].' '.(date('Y', $datetime) + 543); 

		if($time)
		{
				$r .= ' - '.date('H', $datetime).':'.date('i', $datetime);
		}
	
		return $r;
	}
}

if ( ! function_exists('month_th'))
{
	function month_th($month)
	{
		$month_array = array('January','February','March','April','May','June','July','August','September','October','November','December');
		return $month_array[$month-1];
	}
}

if ( ! function_exists('year_th'))
{
	function year_th($year)
	{
		return $year + 543;
	}
}

if ( ! function_exists('mysql_to_relative'))
{
	function mysql_to_relative($datetime){
		$timestamp = mysql_to_unix($datetime);
		$difference = time() - $timestamp;
		$periods = array('Seconds','Minutes','Hour','Day','Week','Month','Year','Ten Years');
		$lengths = array('60','60','24','7','4.35','12','10');
		
		if ($difference >= 0) 
		{ 
			$ending = "ago";
		} 
		else 
		{
			$difference = -$difference;
			$ending = "to go";
		}
		for($j = 0; $difference >= $lengths[$j]; $j++)
		{
			$difference /= $lengths[$j];
		}
		$difference = round($difference);
		$text = "$difference $periods[$j]$ending";
		return $text;
	}
}
if ( ! function_exists('m2h'))
{
	function m2h($mins) 
	{ 
            if ($mins < 0) { 
                $min = Abs($mins); 
            } else { 
                $min = $mins; 
            } 
            $H = Floor($min / 60); 
            $M = ($min - ($H * 60)) / 100; 
            $hours = $H +  $M; 
            if ($mins < 0) { 
                $hours = $hours * (-1); 
            } 
            $expl = explode(".", $hours); 
            $H = $expl[0]; 
            if (empty($expl[1])) { 
                $expl[1] = 00; 
            } 
            $M = $expl[1]; 
            if (strlen($M) < 2) { 
                $M = $M . 0; 
            } 
            $hours = $H . "." . $M; 
            return $hours; 
    } 
}

function DB2Date($date){ 
	if(($date!=NULL)&&($date != '0000-00-00')){
		//list($date,$time) = explode(" ",$Dt);
		list($y,$m,$d) = explode("-",$date);
		return $d."/".$m."/".($y+543);
	}else{ return $date; }
}

function Date2DB($date){
	if(($date!="")&&($date != '0000-00-00')){
		//list($date,$time) = explode(" ",$Dt);
		list($d,$m,$y) = explode("/",$date);
		return ($y-543)."-".$m."-".$d;
	}else{ return $date; }
}
?>